﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Utils;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;



namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public delegate void EventReturnVisitSearch(string filter);

    public partial class ReturnVisitSearchControl : UserControl
    {
        private const string strAll = "所有";

        public EventReturnVisitSearch ReturnVisitSearch { get; set; }

        public ReturnVisitSearchControl()
        {
            InitializeComponent();

            InitCustomer();
            InitEmployee();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (cbeCustomer.SelectedValue == null || cbeEmployee.SelectedValue == null)
                return;

            int iCustomerID = Convert.ToInt32(cbeCustomer.SelectedValue);
            int iEmployeeID = Convert.ToInt32(cbeEmployee.SelectedValue);

            string sFilter = string.Empty;
            if (iCustomerID != -1)
            {
                sFilter = string.Format("{0}={1}", DBEntityCustomerReturnVisit.FieldName.CUSTOMERID, iCustomerID.ToString());
            }

            if (iEmployeeID != -1)
            {
                string filter = string.Format("{0}={1}", DBEntityCustomerReturnVisit.FieldName.CEMPLOYEEID, iEmployeeID.ToString());
                if (string.IsNullOrEmpty(sFilter))
                    sFilter = filter;
                else
                    sFilter += " AND " + filter;
            }

            if (dtiReturnVisit.Text.Trim() != string.Empty)
            {
                string filter = string.Format("{0}='{1}'", DBEntityCustomerReturnVisit.FieldName.CRETURNVISITTIME, dtiReturnVisit.Value.ToString());
                if (string.IsNullOrEmpty(sFilter))
                    sFilter = filter;
                else
                    sFilter += " AND " + filter;
            }

            if (ReturnVisitSearch != null)
            {
                ReturnVisitSearch(sFilter);
            }
        }

        private void InitCustomer()
        {
            string error = string.Empty;
            DataSet ds = new DataSet();
            if (BussinessProxyManager.Instance == null)
            {
                return;
            }
            bool bFlag = BussinessProxyManager.Instance.QueryCustomState(DBEntityCustomInfo.ENUM_CSTATE.C0A, out ds, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取会员信息失败\r\n" + error);
                return;
            }
            if (ds == null || ds.Tables.Count <= 0)
                return;

            DataRow[] drFinds = ds.Tables[0].Select(string.Format("{0}=''", DBEntityCustomInfo.FieldName.CNAME));
            if (drFinds.Length == 0)
            {
                DataRow drNew = ds.Tables[0].NewRow();
                drNew[DBEntityCustomInfo.FieldName.CUSTOMERID] = -1;
                drNew[DBEntityCustomInfo.FieldName.CNAME] = strAll;
                ds.Tables[0].Rows.InsertAt(drNew, 0);
                ds.Tables[0].AcceptChanges();
            }
            cbeCustomer.DataSource = ds.Tables[0].DefaultView;
            cbeCustomer.DisplayMember = DBEntityCustomInfo.FieldName.CNAME;
            cbeCustomer.ValueMember = DBEntityCustomInfo.FieldName.CUSTOMERID;


            cbeCustomer.SelectedIndex = 0;
        }

        private void InitEmployee()
        {
            string error = string.Empty;
            DataSet ds = new DataSet();
            if (BussinessProxyManager.Instance == null)
            {
                return;
            }
            bool bFlag = BussinessProxyManager.Instance.QueryEmployeeState(DBEntityEmployee.ENUM_ESTATE.F0E, out ds, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取经手人信息失败\r\n" + error);
                return;
            }

            DataRow[] drFinds = ds.Tables[0].Select(string.Format("{0}=''", DBEntityEmployee.FieldName.ENAME));
            if (drFinds.Length == 0)
            {
                DataRow drNew = ds.Tables[0].NewRow();
                drNew[DBEntityEmployee.FieldName.EMPLOYEEID] = -1;
                drNew[DBEntityEmployee.FieldName.ENAME] = strAll;
                ds.Tables[0].Rows.InsertAt(drNew, 0);
                ds.AcceptChanges();
            }

            cbeEmployee.DataSource = ds.Tables[0].DefaultView;
            cbeEmployee.DisplayMember = DBEntityEmployee.FieldName.ENAME;
            cbeEmployee.ValueMember = DBEntityEmployee.FieldName.EMPLOYEEID;

            cbeEmployee.SelectedIndex = 0;
        }

        private void ReturnVisitSearchControl_Load(object sender, EventArgs e)
        {
            //InitCustomer();
            //InitEmployee();
        }


    }
}
